package com.ljwy.daka;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;

import com.ljwy.daka.info.DaKaErrorInfo;
import com.ljwy.daka.okgoHelper.GsonConvert;
import com.ljwy.daka.okgoHelper.HttpRequestHelper;
import com.ljwy.daka.okgoHelper.JsonCallback;
import com.ljwy.daka.utils.Constant;
import com.ljwy.daka.utils.ToastUtils;
import com.ljwy.daka.utils.cache.ACacheHelper;
import com.lzy.okgo.model.Response;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditActivity extends BaseActivity  {

    @BindView(R.id.id_et)
    EditText idEt;
    private HashMap<String, String> map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        ButterKnife.bind(this);
        if (!TextUtils.isEmpty(ACacheHelper.getInstance().getString("NO", ""))) {
            goNextActivity();
        }
        map = new HashMap<>();
       // findViewById(R.id.confirm_btn).setOnClickListener(this);
    }

    private void goNextActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }


    @OnClick(R.id.confirm_btn)
    public void onViewClicked() {
        if (TextUtils.isEmpty(idEt.getText().toString().trim())) {
            ToastUtils.showShort("请输入您的团队名");
            return;
        }
        map.clear();
        map.put("no", idEt.getText().toString());
        HttpRequestHelper.postRequest(mContext, Constant.IP + Constant.REGISTER, map, new JsonCallback<String>() {
            @Override
            public void onSuccess(Response<String> response) {
                DaKaErrorInfo errorInfo = GsonConvert.fromJson(response.body(), DaKaErrorInfo.class);
                if (errorInfo.getMsg().getCode()==0) {
                    ACacheHelper.getInstance().putString("NO", idEt.getText().toString());
                    goNextActivity();
                }else {
                   ToastUtils.showShort(errorInfo.getMsg().getInfo());
                }
            }
        });
    }
}